package indi.kider.server.appclient.api;

import indi.kider.common.model.bill.MeterNameChange;
import indi.kider.common.model.bill.MeterRecord;
import indi.kider.common.model.bill.RentMeter;
import indi.kider.server.appclient.service.related.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by shixj on 2018/6/1.
 */
@RestController
@RequestMapping(value = "meter", produces = "application/json;charset=UTF-8")
public class AppClientMeterController {
    @Autowired
    private BillService billService;

    /**
     * @api {PUT} meter/name
     * @apiGroup app-client
     * @apiDescription 修改表名称
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/meter/name
     * {"id":1,"name":"谁的表"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":null}}
     */
    @PutMapping("name")
    public CommonResponse update(@RequestBody MeterNameChange meterNameChange) {
        return billService.update(meterNameChange);
    }

    /**
     * @api {POST} meter/list
     * @apiGroup app-client
     * @apiDescription 查询表具列表
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/meter/list
     * {"size":999,"num":1,"query":{"meterType":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"housesId":69,"meterCode":"110","meterType":1,"locationParam":1,"concentrator":"1","name":"谁的表","rentId":87,"tenantName":"1","tenantTel":"1","houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":null,"expression":null,"enterpriseId":null}],"total":1,"size":1}}
     */
    @PostMapping("list")
    public CommonResponse<PageResponse<List<RentMeter>>> getRentMeterList(@RequestBody PageRequest<RentMeter> request, HttpServletRequest httpServletRequest) {
        Long rentId = Long.valueOf(httpServletRequest.getHeader("rentId"));
        request.getQuery().setRentId(rentId);
        return billService.getRentMeterList(request);
    }

    /**
     * @api {POST} meter/record/list
     * @apiGroup app-client
     * @apiDescription 查询读数列表  meterType：1水2电
     * @apiParamExample 请求样例：
     * http://localhost:9002/app-client/meter/record/list
     * {"size":999,"num":1,"query":{"meterType":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"title":"2018年05月份","thisRead":3.0,"thisReadTime":"2018-05-28 09:54","lastRead":0.0,"lastReadTime":null,"absoluteNumber":3.0},{"title":"2018年05月份","thisRead":3.0,"thisReadTime":"2018-05-28 09:54","lastRead":1.0,"lastReadTime":null,"absoluteNumber":2.0}],"total":2,"size":1}}
     */
    @PostMapping("record/list")
    public CommonResponse<PageResponse<List<MeterRecord>>> getMeterRecordList(@RequestBody PageRequest<RentMeter> request, HttpServletRequest httpServletRequest) {
        Long rentId = Long.valueOf(httpServletRequest.getHeader("rentId"));
        request.getQuery().setRentId(rentId);
        return billService.getMeterRecordList(request);
    }
}
